$(function () {
    let channel = getQueryString("channel");
    let agent = getQueryString("agent");

    let url = '../amountbill/list?1=1';
    if (channel) {
        url += '&channel=' + channel;
    }
    if (agent) {
        url += '&agent=' + agent;
    }

    $("#jqGrid").jqGrid({
        url: url,
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', align: "center", index: 'id', key: true, hidden: true},
            {label: '昵称', align: "center", name: 'userName', width: 100},
            {label: '类型', align: "center", name: 'title', index: 'title', width: 80},
            {label: '创建时间', align: "center", name: 'createTime', index: 'create_time', width: 100},
            {
                label: '类型', align: "center", name: 'type', index: 'type', width: 80,
                formatter: function (value) {
                    return transChance(value);
                }
            },
            {label: '交易金额', align: "center", name: 'amount', index: 'amount', width: 80},
            {
                label: '账单状态', align: "center", name: 'status', index: 'status', width: 80,
                formatter: function (value) {
                    return amountType(value);
                }
            }],

        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

let vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            apiAmountBill: {},
            ruleValidate: {
                name: [
                    {required: true, message: '名称不能为空', trigger: 'blur'}
                ]
            },
            q: {
                name: '',
                phone: '',
                type: ''
            }
        },
        methods: {
            query: function () {
                vm.reload();
            }
            ,
            add: function () {
                vm.showList = false;
                vm.title = "新增";
                vm.apiAmountBill = {};
            }
            ,
            update: function (event) {
                let id = getSelectedRow();
                if (id == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";

                vm.getInfo(id)
            }
            ,
            saveOrUpdate: function (event) {
                let url = vm.apiAmountBill.id == null ? "../amountbill/save" : "../amountbill/update";
                $.ajax({
                    type: "POST",
                    url: url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.apiAmountBill),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            }
            ,
            del: function (event) {
                let ids = getSelectedRows();
                if (ids == null) {
                    return;
                }

                confirm('确定要删除选中的记录？', function () {
                    $.ajax({
                        type: "POST",
                        url: "../amountbill/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            }
            ,
            getInfo: function (id) {
                $.get("../amountbill/info/" + id, function (r) {
                    vm.apiAmountBill = r.apiAmountBill;
                });
            }
            ,
            reload: function (event) {
                vm.showList = true;
                let page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: {'name': vm.q.name, "type": vm.q.type, "phone": vm.q.phone},
                    page: page
                }).trigger("reloadGrid");
                vm.handleReset('formValidate');
            }
            ,
            handleSubmit: function (name) {
                handleSubmitValidate(this, name, function () {
                    vm.saveOrUpdate()
                });
            }
            ,
            handleReset: function (name) {
                handleResetForm(this, name);
            }
        }
    })
;